<?php
/*
 * @chenqiwen
 * 用php实现数据库的连接和相关操作
 */
class DatabaseConnection{
	public  $connection;
	public  $url;
	/*
	 * Connection连接数据库
	 * 返回一个句柄
	 * $server ：主机名
	 * $root：用户名
	 * $password：密码
	 */
	public function __construct($server ,$root,$password = ''){

		return $this->connection = @mysql_connect($server,$root,$password)or die("Unable to connect to the MySQL!");
	}

	/*
	 * Get_Address获取域名
	 */
	public function set_Address(){
        $url =  $_SERVER['HTTP_HOST'];
        $pos = strpos($url,'.');
        $this->url = substr($url,0,$pos);
	}

	/*
	 * Returns_Field返回指定字段值
	 * $database：数据库名称
	 * $tablename：表名
	 * $field：表中的字段名
	 */

	public function returns_Field($database,$tablename,$field,$dbfield){
		@mysql_select_db($database,$this->connection);
		$sql = "SELECT $dbfield FROM $tablename WHERE $field = '$this->url'";
//        echo $sql;die;
		$result = @mysql_query($sql);
		$result = @mysql_fetch_array($result);
		@mysql_close($this->connection);
		return $result[$dbfield];

	}
}
?>